package com.cskaoyan.market.admin.service.impl;

import com.cskaoyan.market.admin.service.MarketAdminCategoryService;

import com.cskaoyan.market.admin.vo.CategoryFatherVo;
import com.cskaoyan.market.admin.vo.CategoryListVo;
import com.cskaoyan.market.admin.vo.CategoryVo;
import com.cskaoyan.market.db.domain.MarketCategory;
import com.cskaoyan.market.db.domain.MarketCategoryExample;
import com.cskaoyan.market.db.domain.MarketUserExample;
import com.cskaoyan.market.db.mapper.MarketCategoryMapper;
import com.cskaoyan.market.util.MarketConstant;
import com.cskaoyan.market.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MarketAdminCategoryServiceImpl implements MarketAdminCategoryService {

    @Override
    public List<CategoryListVo> getList() {
        SqlSession session = MyBatisUtil.getSession();
        MarketCategoryMapper mapper = session.getMapper(MarketCategoryMapper.class);
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        List<MarketCategory> marketCategories = mapper.selectByExample(marketCategoryExample);
        session.commit();
        session.close();
        Map<Integer, List<MarketCategory>> categoriesGroupByPid = groupByPid(marketCategories);
        List<MarketCategory> fatherList = categoriesGroupByPid.get(MarketConstant.MARKET_FATHER_CATEGORY);

        List<CategoryListVo> list = new ArrayList<>();
        for (MarketCategory father : fatherList) {
            CategoryListVo fatherCategoryVo = new CategoryListVo();
            fatherCategoryVo.setId(father.getId());
            fatherCategoryVo.setName(father.getName());
            fatherCategoryVo.setKeywords(father.getKeywords());
            fatherCategoryVo.setDesc(father.getDesc());
            fatherCategoryVo.setIconUrl(father.getIconUrl());
            fatherCategoryVo.setLevel(father.getLevel());
            fatherCategoryVo.setPicUrl(father.getPicUrl());
            List<MarketCategory> childrenList = categoriesGroupByPid.get(father.getId());
            List<CategoryListVo> childrenOfFatherVo = new ArrayList<>();
            if (childrenList != null) {
                for (MarketCategory child : childrenList) {
                    CategoryListVo childrenCategoryVo = new CategoryListVo();
                    childrenCategoryVo.setId(child.getId());
                    childrenCategoryVo.setName(child.getName());
                    childrenCategoryVo.setKeywords(child.getKeywords());
                    childrenCategoryVo.setDesc(child.getDesc());
                    childrenCategoryVo.setIconUrl(child.getIconUrl());
                    childrenCategoryVo.setLevel(child.getLevel());
                    childrenCategoryVo.setPicUrl(child.getPicUrl());
                    childrenOfFatherVo.add(childrenCategoryVo);
                }
            }
            fatherCategoryVo.setChildren(childrenOfFatherVo);
            list.add(fatherCategoryVo);
        }
        return list;
    }

    @Override
    public MarketCategory createCategory(String desc, String iconUrl, String keywords, String level, String name, String picUrl, Integer pid) {
        SqlSession session = MyBatisUtil.getSession();
        MarketCategoryMapper mapper = session.getMapper(MarketCategoryMapper.class);
        MarketCategory marketCategory = new MarketCategory();
        marketCategory.setAddTime(LocalDateTime.now());
        marketCategory.setDesc(desc);
        marketCategory.setIconUrl(iconUrl);
        marketCategory.setId(marketCategory.getId());
        marketCategory.setKeywords(keywords);
        marketCategory.setLevel(level);
        marketCategory.setName(name);
        marketCategory.setPicUrl(picUrl == null ? "" : picUrl);
        marketCategory.setPid(pid);
        marketCategory.setUpdateTime(LocalDateTime.now());
        mapper.insertSelective(marketCategory);
        session.commit();
        session.close();
        return marketCategory;

    }

    @Override
    public void updateCategory(String desc, String iconUrl, Integer id, String keywords, String level, String name, String picUrl, Integer pid) {
        SqlSession session = MyBatisUtil.getSession();
        MarketCategoryMapper mapper = session.getMapper(MarketCategoryMapper.class);
        MarketCategory marketCategory = mapper.selectByPrimaryKey(id);
        marketCategory.setUpdateTime(LocalDateTime.now());
        marketCategory.setDesc(desc);
        marketCategory.setIconUrl(iconUrl);
        marketCategory.setKeywords(keywords);
        marketCategory.setName(name);
        marketCategory.setLevel(level);
        marketCategory.setPicUrl(picUrl == null ? "" : picUrl);
        marketCategory.setPid(pid);
        mapper.updateByPrimaryKey(marketCategory);
        session.commit();
        session.close();
    }

    @Override
    public List<CategoryFatherVo> getL1() {
        SqlSession session = MyBatisUtil.getSession();
        MarketCategoryMapper mapper = session.getMapper(MarketCategoryMapper.class);
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        List<MarketCategory> marketCategories = mapper.selectByExample(marketCategoryExample);
        session.commit();
        session.close();
        Map<Integer, List<MarketCategory>> listMap = groupByPid(marketCategories);
        List<MarketCategory> categories = listMap.get(MarketConstant.MARKET_FATHER_CATEGORY);
        List<CategoryFatherVo> categoryFatherVoList = new ArrayList<>();

        for (MarketCategory category : categories) {
            CategoryFatherVo categoryFatherVo = new CategoryFatherVo();
            categoryFatherVo.setValue(category.getId());
            categoryFatherVo.setLabel(category.getName());

            categoryFatherVoList.add(categoryFatherVo);
        }
        return categoryFatherVoList;
    }

    @Override
    public void deleteCategory(Integer id) {
        SqlSession session = MyBatisUtil.getSession();
        MarketCategoryMapper mapper = session.getMapper(MarketCategoryMapper.class);
        mapper.deleteByPrimaryKey(id);
        session.commit();
        session.close();
    }

    private Map<Integer, List<MarketCategory>> groupByPid(List<MarketCategory> categories) {
        Map<Integer, List<MarketCategory>> result = new HashMap<>();
        for (MarketCategory category : categories) {
            List<MarketCategory> categoriesList = result.get(category.getPid());
            if (categoriesList == null) {
                categoriesList = new ArrayList<>();
                result.put(category.getPid(), categoriesList);
            }
            categoriesList.add(category);
        }
        return result;
    }
}
